﻿using System;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 粒子效果播放
    /// </summary>
    public class EffectPlayMB : MonoBehaviour
    {
        private ParticleSystem[] systemList;
        private Animator[] animatorList;
        private Renderer[] mRendererList;

        [SortingLayerAttribute] public string layerName;

        public float playTime;
        public float playMaxTime;
        private bool isPlay = false;
        public event Action OnPlayComplete;
        public event Action OnPlayStart;
        public event Action OnPlayPause;
        public event Action<float> OnPlayTime;


        private void Awake()
        {
            systemList = GetComponentsInChildren<ParticleSystem>();
            animatorList = GetComponentsInChildren<Animator>();
            CalMaxPlayTime();
            mRendererList = GetComponentsInChildren<Renderer>();
            SetSortingLayer(layerName);
        }

        public void SetSortingLayer(string layerName)
        {
            foreach (var item in mRendererList)
            {
                item.sortingLayerName = layerName;
            }
        }

        public void SetLayer(int layer)
        {
            foreach (var item in mRendererList)
            {
                item.sortingLayerID = layer;
            }
        }

        public void AllPlay()
        {
            foreach (var item in systemList)
            {
                item.Play();
            }

            foreach (var item in animatorList)
            {
                item.enabled = true;
            }

            playTime = 0f;
            isPlay = true;
            if (OnPlayStart != null)
            {
                OnPlayStart();
            }
        }

        public void AllStop()
        {
            foreach (var item in systemList)
            {
                item.Stop();
            }

            foreach (var item in animatorList)
            {
                item.enabled = false;
            }

            playTime = 0f;
            isPlay = false;
            if (OnPlayPause != null)
            {
                OnPlayPause();
            }
        }

        public void IsLoop(bool loop)
        {
            foreach (var item in systemList)
            {
                var main = item.main;
                main.loop = loop;
            }
        }

        protected void FixedUpdate()
        {
            CheckPaySus2();
        }

        private void CheckPaySus1()
        {
            if (isPlay)
            {
                playTime += Time.fixedDeltaTime;
                bool allStop = true;
                for (int i = 0; i < systemList.Length; i++)
                {
                    if (!systemList[i].isStopped)
                    {
                        allStop = false;
                        break;
                    }
                }

                if (allStop)
                {
                    if (OnPlayComplete != null)
                    {
                        OnPlayComplete();
                    }

                    isPlay = false;
                }

                if (OnPlayTime != null)
                {
                    OnPlayTime(playTime);
                }
            }
        }

        private void CheckPaySus2()
        {
            if (isPlay)
            {
                playTime += Time.fixedDeltaTime;
                if (playTime >= playMaxTime)
                {
                    if (OnPlayComplete != null)
                    {
                        OnPlayComplete();
                    }

                    isPlay = false;
                }

                if (OnPlayTime != null)
                {
                    OnPlayTime(playTime);
                }
            }
        }


        private void CalMaxPlayTime()
        {
            foreach (var item in systemList)
            {
                if (item.emission.enabled)
                {
                    if (item.emission.rateOverTimeMultiplier <= 0)
                    {
                        playMaxTime = item.main.startDelayMultiplier + item.main.startLifetimeMultiplier + 1;
                    }
                    else
                    {
                        playMaxTime = item.main.startDelayMultiplier +
                                      UnityEngine.Mathf.Max(item.main.duration, item.main.startLifetimeMultiplier) + 1;
                    }
                }
            }


            foreach (var item in animatorList)
            {
                var control = item.runtimeAnimatorController;
                if (control == null)
                {
                    return;
                }

                AnimationClip[] clips = control.animationClips;
                foreach (var v in clips)
                {
                    playMaxTime = playMaxTime > v.length ? playMaxTime : v.length;
                }
            }
        }
    }
}